package com.retired_serviceman_management.service.impl;

import com.retired_serviceman_management.entity.VocationalInfo;
import com.retired_serviceman_management.dao.VocationalInfoDao;
import com.retired_serviceman_management.service.VocationalInfoService;
import javafx.geometry.VPos;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * (VocationalInfo)表服务实现类
 *
 * @author makejava
 * @since 2023-03-07 14:31:06
 */
@Service("vocationalInfoService")
public class VocationalInfoServiceImpl implements VocationalInfoService {
    @Resource
    private VocationalInfoDao vocationalInfoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param infoId 主键
     * @return 实例对象
     */
    @Override
    public VocationalInfo queryById(Integer infoId) {
        return this.vocationalInfoDao.queryById(infoId);
    }

    /**
     * 分页查询
     *
     * @param vocationalInfo 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */


    /**
     * 新增数据
     *
     * @param vocationalInfo 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(VocationalInfo vocationalInfo) {
        VocationalInfo vocationalInfos = vocationalInfoDao.queryByAccount(vocationalInfo.getComAccount());
        if(vocationalInfos!=null) return 2;
        return vocationalInfoDao.insert(vocationalInfo);
    }

    @Override
    public int insert1(String comAccount, String infoContent, MultipartFile[] files) throws IOException {
        VocationalInfo vocationalInfos = vocationalInfoDao.queryByAccount(comAccount);//判断当前账号是否存在职业信息
        if(vocationalInfos!=null) return 2;
        System.out.println("comAccount = " + comAccount);
        System.out.println("infoContent = " + infoContent);
        System.out.println("files = " + files);
        VocationalInfo vocationalInfo = new VocationalInfo();
        vocationalInfo.setInfoContent(infoContent);
        vocationalInfo.setComAccount(comAccount);
        int count = 1;
        for (MultipartFile mf : files) {
            if (!mf.isEmpty()) {
                // 使用UUID给图片重命名，并去掉四个“-”
                String name = UUID.randomUUID().toString().replaceAll("-", "");
                System.out.println("name = " + name);
                // 获取文件的扩展名
                String ext = FilenameUtils.getExtension(mf.getOriginalFilename());
                System.out.println("ext = " + ext);
                // 设置图片上传路径
                String url = "D:\\Java\\project\\Retired_Serviceman_Management\\src\\main\\webapp\\upload";
                System.out.println("url = " + url);
                // 以绝对路径保存重名命后的图片
                mf.transferTo(new File(url + "/" + name + "." + ext));
                // 把图片存储路径保存到数据库
                String path = "/upload/" + name + "." + ext;
                System.out.println("path = " + path);
                System.out.println("count = " + count);
                if (count == 1) {
                    vocationalInfo.setImg1(path);
                } else if (count == 2) {
                    vocationalInfo.setImg2(path);
                } else if (count == 3) {
                    vocationalInfo.setImg3(path);
                }
            }
            count++;
        }
        vocationalInfo.setInfoStatus("F");
        System.out.println("vocationalInfo = " + vocationalInfo);
        return vocationalInfoDao.insert(vocationalInfo);
    }

    /**
     * 修改数据
     *
     * @param vocationalInfo 实例对象
     * @return 实例对象
     */
    @Override
    public VocationalInfo update(VocationalInfo vocationalInfo) {
        this.vocationalInfoDao.update(vocationalInfo);
        return this.queryById(vocationalInfo.getInfoId());
    }

    /**
     * 通过主键删除数据
     *
     * @param infoId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer infoId) {
        return this.vocationalInfoDao.deleteById(infoId) > 0;
    }

    @Override
    public VocationalInfo queryByAccount(String comAccount) {
        VocationalInfo vocationalInfos = vocationalInfoDao.queryByAccount(comAccount);
        vocationalInfos.setImg1("/"+vocationalInfos.getImg1());
        vocationalInfos.setImg2("/"+vocationalInfos.getImg2());
        vocationalInfos.setImg3("/"+vocationalInfos.getImg3());
            if(vocationalInfos.getInfoStatus().equals("T")){
                vocationalInfos.setInfoStatus("审核已通过");
            }
            else if(vocationalInfos.getInfoStatus().equals("R")){
                vocationalInfos.setInfoStatus("审核未通过");
            }
            else if(vocationalInfos.getInfoStatus().equals("F")){
                vocationalInfos.setInfoStatus("待审核");
            }

        return vocationalInfos;
    }

    @Override
    public Integer Submit_vocation(Integer infoId) {
        VocationalInfo vocationalInfo1 = vocationalInfoDao.queryById(infoId);
        if(vocationalInfo1.getInfoStatus().equals("T")) return 2;//审核已经通过，无需再次提交审核
        else if(vocationalInfo1.getInfoStatus().equals("F")) return 3;//当前还为未审核，请耐心等待
        else if(vocationalInfo1.getInfoStatus().equals("R")){//审核被拒绝，可以重新发起审核
            Integer integer = vocationalInfoDao.Submit_vocation(infoId);
            return integer;//0 提交失败  1提交成功
        }
        return null;
    }

    @Override
    public Integer Agree_vocation(Integer infoId) {
        Integer status = vocationalInfoDao.updateStatusById(infoId);
        return status;
    }

    @Override
    public Integer Disagree_vocation(Integer infoId) {
        Integer status = vocationalInfoDao.updateStatusById2(infoId);
        return status;
    }

    @Override
    public List<VocationalInfo> queryByStatus() {
        List<VocationalInfo> vocationalInfos = vocationalInfoDao.queryByStatus();
        for(int i=0;i<vocationalInfos.size();i++){
            if(vocationalInfos.get(i).getInfoStatus().equals("T")){
                vocationalInfos.get(i).setInfoStatus("审核已通过");
            }
            else if(vocationalInfos.get(i).getInfoStatus().equals("F")){
                vocationalInfos.get(i).setInfoStatus("待审核");
            }
        }
        return vocationalInfos;
    }

    @Override
    public VocationalInfo view_detail(String comAccount) {
        VocationalInfo vocationalInfo = vocationalInfoDao.queryByAccount(comAccount);
        if(vocationalInfo!=null&&vocationalInfo.getInfoStatus().equals("T")) vocationalInfo.setInfoStatus("已通过");
        if(vocationalInfo!=null&&vocationalInfo.getInfoStatus().equals("F")) vocationalInfo.setInfoStatus("待审核");
        if(vocationalInfo!=null&&vocationalInfo.getInfoStatus().equals("R")) vocationalInfo.setInfoStatus("未通过");
        return vocationalInfo;
    }

    @Override
    public Integer update_vocation(String comAccount, String infoContent, MultipartFile[] files) throws IOException {
        System.out.println("files = " + files);
        VocationalInfo vocationalInfo = new VocationalInfo();
        vocationalInfo.setComAccount(comAccount);
        vocationalInfo.setInfoContent(infoContent);
        vocationalInfo.setInfoStatus("F");
        if (files!=null) {
            int count = 1;
            for (MultipartFile mf : files) {
                if (!mf.isEmpty()) {
                    // 使用UUID给图片重命名，并去掉四个“-”
                    String name = UUID.randomUUID().toString().replaceAll("-", "");
                    System.out.println("name = " + name);
                    // 获取文件的扩展名
                    String ext = FilenameUtils.getExtension(mf.getOriginalFilename());
                    System.out.println("ext = " + ext);
                    // 设置图片上传路径
                    String url = "D:\\Java\\project\\Retired_Serviceman_Management\\src\\main\\webapp\\upload";
                    System.out.println("url = " + url);
                    // 以绝对路径保存重名命后的图片
                    mf.transferTo(new File(url + "/" + name + "." + ext));
                    // 把图片存储路径保存到数据库
                    String path = "upload/" + name + "." + ext;
                    System.out.println("path = " + path);
                    System.out.println("count = " + count);
                    if (count == 1) {
                        vocationalInfo.setImg1(path);
                    } else if (count == 2) {
                        vocationalInfo.setImg2(path);
                    } else if (count == 3) {
                        vocationalInfo.setImg3(path);
                    }
                }
                count++;
            }
            int i = vocationalInfoDao.updateBycomAccount(vocationalInfo);
            return i;
        }
        else {
            vocationalInfo.setImg1(null);
            vocationalInfo.setImg2(null);
            vocationalInfo.setImg3(null);
            int i = vocationalInfoDao.updateBycomAccount(vocationalInfo);
            return i;
        }

    }

    @Override
    public VocationalInfo Enterprise_detail(String comAccount) {
        VocationalInfo vocationalInfo = vocationalInfoDao.queryByAccount1(comAccount);
        if (vocationalInfo==null) return null;
        vocationalInfo.setImg1("/"+vocationalInfo.getImg1());
        vocationalInfo.setImg2("/"+vocationalInfo.getImg2());
        vocationalInfo.setImg3("/"+vocationalInfo.getImg3());
        return vocationalInfo;
    }
}
